package cn.ffcs.base.web;

import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import cn.ffcs.base.model.BaseEntity;
import cn.ffcs.util.ext.Expression;

public class ExtBaseAction<B extends BaseEntity> extends BaseAction<B>
{
    
    /**
     * 获取分页数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String findByPage()
    {
              
        //如果第一次装置，默认参数
        if (pageControl == null)
        {
            pageControl = new PageControl();
        }
        
        pageControl = baseService.findByPage(combineCondExp(), pageControl.getPageNo(),
                pageControl.getPageSize());
        return SUCCESS;
    }
    
    protected Expression combineCondExp(){
        Expression exp=new Expression();
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext()) 
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Expression temp=new Expression();
                    if (key.equalsIgnoreCase("name")){
                        temp.like(key, super.getConds().get(key).toString());
                    }else{
                        temp.eq(key, super.getConds().get(key));                        
                    }
                    
                    if (exp == null || exp.getSqlString()==null)
                    {
                        exp = temp;
                    }
                    else
                    {
                        exp.and(exp, temp);
                    }
                }
            }
        }
        return exp;
    } 
}
